<script setup>
import {ref} from 'vue'
import {useRouter} from 'vue-router'

import {Toast} from 'vant'
import PopupBox from "@/components/PopupBox.vue";
import requestUtil from "@/utils/request.js";
import {encrypt} from "@/utils/jsencrypt.js";
import {ElMessage} from "element-plus";

const router = useRouter()
const email = ref('')
const password = ref('')
const password2 = ref('')
const username = ref('')
let message = ref('')
let show = ref(false)
const onSubmit = async () => {

  // 检查密码是否匹配
  if (password.value !== password2.value) {
    // 如果密码不匹配，弹出失败提示框
    ElMessage.warning('密码不匹配');
    return
  }

  // 检查是否为空字段
  if (email.value === '' || password.value === '' || password2.value === '') {
    // 如果有字段为空，弹出失败提示框
    // Toast.fail('请完整填写所有字段。')
    return
  }
  let userInfo = ref({
    email: email.value,
    username: username.value,
    password: password.value,
  })

  // 注册成功后
  let res = await requestUtil.post("/app/register/",{userInfo:userInfo.value})
  
  if (res.data.code!== 200) {
  // 注册成功后，跳转到登录页面
    ElMessage.warning('账号已存在')
  }
  else{
    ElMessage.success('注册成功')
    router.push("/login");
  }

}


const Back = () => {
  router.back()

}
</script>

<template>
  <div class="register">
    <div class="register-head">
      <button @click="Back">‹</button>
    </div>

    <div class="register-content">
      <h1>注册</h1>
      <p>注册您的账号继续</p>
      <van-form class="register-box1">
        <van-cell-group inset>
          <van-field
            v-model="email"
            name="邮箱"
            label="邮箱"
            placeholder="请输入邮箱"
            :rules="[{
                  required: true,
                  message: '请填写邮箱'
              }, {
                  pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/,
                  message: '邮箱格式不正确'
              }]"
          />
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="请输入用户名"
            :rules="[{
                  required: true,
                  message: '请输入用户名'
              },]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="请输入密码"
            :rules="[{
                  required: true,
                  message: '请填写密码'
              }, {
                  pattern: /^.{6,}$/,
                  message: '密码长度不能小于6位'
              }]"
          />
          <van-field
            v-model="password2"
            type="password"
            name="密码"
            label="密码"
            placeholder="请再次输入密码"
            :rules="[{
                  required: true,
                  message: '请填写密码'
              }, {
                  pattern: /^.{6,}$/,
                  message: '密码长度不能小于6位'
              }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit" @click="onSubmit">立即注册</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<style scoped>
.register {
  background-image: url("/img/登录/login1.png");
  background-size: cover;
  background-position: center;
  width: 100%;
  height: 100vh;
}

.register-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 16px;
  position: fixed;
  top: 0;
  background-color: rgba(0, 101, 255, 0.8);
  z-index: 10;
}

.register-head button {
  background-color: transparent;
  border: none;
  font-size: 28px;
  color: white;
}

.register-content {
  margin-top: 70px;
  background-color: rgba(255, 255, 255, 0.9);
  width: 100%;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  padding: 20px;
  box-sizing: border-box;
}

.register-content h1 {
  text-align: center;
  font-size: 32px;
  color: #333;
}

.register-content p {
  text-align: center;
  font-size: 16px;
  color: #666;
  margin-bottom: 30px;
}

.van-button--primary {
  background-color: #0065ff;
  border-radius: 30px;
  font-size: 16px;
  font-weight: bold;
}

.van-button--primary:hover {
  background-color: #0054cc;
}

.van-field__control {
  border-radius: 10px;
  padding: 10px;
  border: 1px solid #ddd;
}

.van-field__control:focus {
  border-color: #0065ff;
}

.van-cell-group {
  margin-bottom: 30px;
}

.van-field__error {
  font-size: 14px;
  color: red;
  margin-top: 5px;
  text-align: left;
}
</style>
